import EventManager from "../../../Common/EventManager";
import Loader from "../../../Common/Loader";
import { EventType } from "../../../GameSpecial/GameEventType";
import gameConfig from "../../config/gameConfig";
import { mgrs, sers } from "../../system/OverallSystem ";
import SignInItem from "./SignInItem";
import gameListItem from "./gameListItem";
import PageBase from "./pageBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SignIn extends PageBase {

    public pageName: string = "签到页面";

    @property(cc.Node)
    item: cc.Node = null

    @property(cc.Node)
    scrContent: cc.Node = null
    

    @property(cc.Button)
    qiandaoBtn: cc.Button = null

    @property(cc.Button)
    fanbeiqiandaoBtn:cc.Button = null

    onLoad() {

    }

    start() {

        this.initList()
        this._init();

       this.initBtn()
    //    EventManager.on(EventType.UIEvent.SignInList,this.initBtn.bind(this),this)
    }

    initList(){
        let s = sers().userSrv.getSignInList()
        let d = '首次签到奖励'
        if(s.one){
            d = '循环签到奖励'
        }
        console.log(d)
        Loader.loadBundleRes('home', 'json/'+d, (res) => {
            console.log(res.json)
            let data = res.json

            Loader.loadBundleRes('home', 'json/物品', (res2) => {
                let data2 = res2.json
                if(!this || !this.node)return
                this.initGame(data,data2)
            })

        })
    }

    initBtn(){
       
        let nowTime =  new Date().getTime();
        let s = sers().userSrv.getSignInList()
        if(s.lastTime){
            let can = this.isSameDay(nowTime, s.lastTime)
            this.qiandaoBtn.interactable = !can
            this.fanbeiqiandaoBtn.interactable = !can
            console.log('是否可签到'+!can)
            if(!can && s.list.length>=7){
                s.list = []
                s.one = true
                sers().userSrv.setSignInList(s)
                this.initList()
            }
        }
    
    }

    listData = null
    nowData = null
    initGame(data,wupinData) {

        // this.scrContent.removeAllChildren()
        let s = sers().userSrv.getSignInList()
        this.fanbei = false
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                let node =  this.scrContent.children[+key-1]
                if(node){
                    element.key = key
                    let d = {
                        data:element,
                        wupinData
                    }
                    node.getComponent(SignInItem).setData(d)
                    if(s.list.indexOf(+key)<0 && !this.nowData){
                        this.nowData = d

                        if(this.nowData.data['奖励物品ID'] == '1001'){
                            this.fanbeiqiandaoBtn.node.active = true
                        }else{
                            this.fanbeiqiandaoBtn.node.active = false
                        }
                    }
                }
            }
        }

    }

    protected _init(): void {

        this.open();
    }

    isSameDay(timestamp1, timestamp2) {
        const date1 = new Date(timestamp1);
        const date2 = new Date(timestamp2);
      
        return (
          date1.getFullYear() === date2.getFullYear() &&
          date1.getMonth() === date2.getMonth() &&
          date1.getDate() === date2.getDate()
        );
      }
      
    
    OnBtnConfirm() {
       
    }

    fanbei = false
    fabeiqian(){
        sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
            this.fanbei = true
            this.qiandao()
        }, (err) => {
        }, null, {
            scene: '签到',
            event: '翻倍签到',
          },'common_video');
    }

    qiandao(){
        mgrs().soundMgr.playBtnSound();
        let s = sers().userSrv.getSignInList()
        s.lastTime = new Date().getTime();
        s.list.push(+this.nowData.data.key)

        if(!window['tt'] && !window['wx']){
            s.lastTime = 1
        }

        sers().userSrv.setSignInList(s)

        let data = this.nowData.data
        let wupin = this.nowData.wupinData

        


       

        // cc.log('奖励ID'+data['奖励物品ID'])
        if(data['奖励物品ID'] == '1001'){
            let n = +data['奖励物品数量']
            if(this.fanbei){
                n = 5 * n
            }
            sers().userSrv.addCoin(n)
            sers().uiSrv.showMessage(`获得${wupin[data['奖励物品ID']]['物品名称']}X${n}`)
        }else {
            let s = sers().userSrv.getZhutiData()
            s.push(wupin[data['奖励物品ID']]['储存'])
            s.push('3_2')
            sers().userSrv.setZhutiData(s)

            sers().uiSrv.showMessage(`获得${wupin[data['奖励物品ID']]['物品名称']}X${data['奖励物品数量']}`)
        }

       EventManager.emit(EventType.UIEvent.SignInList)
       this.initBtn()

       if(!window['tt'] && !window['wx']){
            cc.log('init')
            this.nowData = null
            this.initList()
            this.initBtn()
        }
    }

    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        this.onlyClose();
    }

    // update (dt) {}
}
